import React, { Component } from 'react';
import { Route } from 'react-router-dom';
import { getToken } from 'utils/storage';

export default class AuthRoute extends Component {
	route = (props) => {
		const { component: Component } = this.props;
		return getToken() ? (
			<Component {...props} />
		) : (
			props.history.push('/login')
		);
	};
	render() {
		const { path, component, ...restProps } = this.props;

		return (
			<Route
				path={path}
				{...restProps}
				render={(props) => this.route(props)}
			/>
		);
	}
}
